package com.spike.service;

import com.spike.mapper.UserMapper;
import com.spike.model.User;
import com.spike.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

public class UserService {
    public User findByName(String username){

        try {
            MyBatisUtils myBatisUtils = new MyBatisUtils();
            SqlSession sqlSession = myBatisUtils.getSqlSession();
            return sqlSession.getMapper(UserMapper.class).findByName(username);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public User login(String username, String password) {
        try {
            MyBatisUtils myBatisUtils = new MyBatisUtils();
            SqlSession sqlSession = myBatisUtils.getSqlSession();
            return sqlSession.getMapper(UserMapper.class).login(username, password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public User findByPhone(String phone) {
        try {
            MyBatisUtils myBatisUtils = new MyBatisUtils();
            SqlSession sqlSession = myBatisUtils.getSqlSession();
            return sqlSession.getMapper(UserMapper.class).findByPhone(phone);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
